<?php
namespace api\modules\v1\controllers;
use api\common\controllers\UserController as U;
use api\modules\v1\models\News;
use yii\base\Exception;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
use Yii;

class CollectController extends U
{
    public $modelClass = 'api\modules\v1\models\Collect';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }


    /*
     * 收藏信息
     * @return array
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionCreate(){
        $model = new $this->modelClass();
        $model->attributes = Yii::$app->request->post();
        $model->uid = Yii::$app->user->identity->id;
        $model->time = date('Y-m-d H:i:s');
        if($model->findOne(['content_id'=>$model->content_id,'catid'=>$model->catid,'uid'=>Yii::$app->user->identity->id])){
            throw new HttpException(401,'该文章已存在收藏列表',0);
        }
        if($model->save()){
            $model->PushTj($model->content_id,$model->catid,1);
            return [
                "message"=>"收藏成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }


    /*
     * 取消收藏
     * @return array
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionDel(){
        try{
            $model = new $this->modelClass();
            $content_id = Yii::$app->request->post('content_id');
            $catid = Yii::$app->request->post('catid');
            $model->PushTj($content_id,$catid,0);
            if(empty($content_id)) throw new HttpException(401,'参数content_id不能为空',0);
            if(empty($catid)) throw new HttpException(401,'参数catid不能为空',0);
            if($model->deleteAll(['content_id'=>$content_id,'catid'=>$catid,'uid'=>Yii::$app->user->identity->id])){
                $model->PushTj($content_id,$catid,0);
                return [
                    "message"=>"取消收藏成功!",
                ];
            }elseif (!$model->hasErrors()) {
                throw new ServerErrorHttpException('由于不明原因未能创建对象.');
            }else{
                throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
            }
        }catch (Exception $e){}
    }



    /*
     * 获取收藏列表
     * @param $id
     * @param $catid
     * @return array|\yii\db\ActiveRecord[]
     */
    public function actionList(){
        $modelClass = $this->modelClass;
        $searchModel = new $modelClass();
        $dataProvider = $searchModel->getList();
        return $dataProvider;
    }


    /*
     * 栏目收藏
     * @return array
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionCatecreate(){
        $this->modelClass = 'api\modules\v1\models\User';
        $model = $this->findModel(Yii::$app->user->identity->id);
        $model->scenario='collect';
        $model->attributes = Yii::$app->request->post();
        if($model->sc()){
            return [
                "message"=>"栏目收藏成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

}